package com.adgox.tiantianbiting.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Messenger;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.bean.SubProgramBean;
import com.adgox.tiantianbiting.home.programDetail.ProgramDetailActivity;
import com.adgox.tiantianbiting.music.MusicService;
import com.adgox.tiantianbiting.utils.TimeUtils;
import com.bumptech.glide.Glide;

import java.io.PipedReader;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by tang on 2018/12/26.
 */

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> implements View.OnClickListener, SeekBar.OnSeekBarChangeListener {

    private List<SubProgramBean> datas;
    private int playPostion = -1;
    private int playCurrentTime = 0;
    private int playTotalTime = 0;
    private boolean isPlaying = false;
    private Context context;

    public CategoryAdapter(List<SubProgramBean> datas) {
        this.datas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view = LayoutInflater.from(context).inflate(R.layout.item_rv_category, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        SubProgramBean bean = datas.get(position);
        Glide.with(holder.itemView.getContext()).load(bean.getImageUrl()).into(holder.imgBook);
        holder.tvBookName.setText(bean.getTitle());
        holder.tvBookStatus.setText("(" + bean.getStatusStr() + ")");
        holder.tvDescribe.setText(bean.getDescribe());
        holder.tvSeriesNum.setText("共 " + bean.getTotalSeason() + "集");
        holder.tvListenNum.setText(bean.getListenNum() + "人正在听");
        holder.tvTry.setText(bean.getCategory() == 1 ? "免费听" : "");

        if (playPostion == position){
            holder.llProgress.setVisibility(View.VISIBLE);
            holder.tvCurrent.setText(TimeUtils.MSToTime(playCurrentTime));
            holder.tvTotal.setText(TimeUtils.MSToTime(playTotalTime));
            holder.seekBar.setMax(playTotalTime);
            holder.seekBar.setProgress(playCurrentTime);
            holder.imgPlay.setImageResource(isPlaying?R.drawable.icon_delay2:R.drawable.icon_play2);
        } else {
            holder.llProgress.setVisibility(View.GONE);
            holder.imgPlay.setImageResource(R.drawable.icon_play2);
        }

        holder.imgPlay.setTag(position);
        holder.itemView.setTag(position);
        holder.seekBar.setOnSeekBarChangeListener(this);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    public void addDatas(List<SubProgramBean> list) {
        Log.e("TAGhhhhh", "addDatas: "+list.size() );
        datas.addAll(list);
        notifyDataSetChanged();
    }

    public void setDatas(List<SubProgramBean> list){
        this.datas = list;
        notifyDataSetChanged();
    }

    public void clear() {
        datas.clear();
        notifyDataSetChanged();
    }

    public void setPlayPostion(int playPostion){
        this.playPostion = playPostion;
        notifyDataSetChanged();
    }

    public void setPlayCurrentTime(int playCurrentTime){
        this.playCurrentTime = playCurrentTime;
        notifyDataSetChanged();
    }

    public void setPlayTotalTime(int playTotalTime){
        this.playTotalTime = playTotalTime;
        notifyDataSetChanged();
    }

    public void isPlaying (boolean isPlaying){
        this.isPlaying = isPlaying;
        notifyDataSetChanged();
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser){
            Intent intentService = new Intent(context, MusicService.class);
            intentService.putExtra("option", "seekPlay");
            intentService.putExtra("messenger", new Messenger(new Handler()));
            intentService.putExtra("progress", progress);
            context.startService(intentService);
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }

    public interface onItemClickListener {
        void onItemClick(View view, ViewType viewType, int position);
    }

    public enum ViewType {
        PLAY,
        ITEM
    }

    public onItemClickListener listener = null;

    public void setOnItemClickListener(onItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View view) {
        int position = (int) view.getTag();
        if (listener != null) {
            if (view.getId() == R.id.img_play) {
                listener.onItemClick(view, ViewType.PLAY, position);
            } else {
                listener.onItemClick(view, ViewType.ITEM, position);
            }

        }
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.img_book)
        ImageView imgBook;
        @BindView(R.id.img_play)
        ImageView imgPlay;
        @BindView(R.id.tv_current)
        TextView tvCurrent;
        @BindView(R.id.seekBar)
        SeekBar seekBar;
        @BindView(R.id.tv_total)
        TextView tvTotal;
        @BindView(R.id.ll_progress)
        LinearLayout llProgress;
        @BindView(R.id.tv_book_name)
        TextView tvBookName;
        @BindView(R.id.tv_book_status)
        TextView tvBookStatus;
        @BindView(R.id.tv_describe)
        TextView tvDescribe;
        @BindView(R.id.tv_series_num)
        TextView tvSeriesNum;
        @BindView(R.id.tv_listen_num)
        TextView tvListenNum;
        @BindView(R.id.tv_try)
        TextView tvTry;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            imgPlay.setOnClickListener(CategoryAdapter.this);
            itemView.setOnClickListener(CategoryAdapter.this);
        }
    }
}
